package cn.woochen.mvvmframe.activity

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.view.View
import cn.woochen.comlib.photo.GlideEngine
import cn.woochen.comlib.photo.PhotoHelper
import cn.woochen.comlib.util.GlideUtil
import cn.woochen.mvvmframe.R
import cn.woochen.mvvmframe.config.BaseActivity
import cn.woochen.mvvmframe.databinding.ActivityPhotoBinding
import cn.woochen.mvvmframe.util.logcom
import cn.woochen.mvvmframe.viewmodels.EmptyViewModel
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.luck.picture.lib.basic.PictureSelector
import com.luck.picture.lib.config.PictureMimeType
import com.luck.picture.lib.config.SelectMimeType
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.interfaces.OnResultCallbackListener
import com.luck.picture.lib.utils.PictureFileUtils
import java.io.File


/**
 *类描述：相册选择
 *@author woochen
 *@time 2021/8/2 17:32
 */
class PhotoActivity : BaseActivity<ActivityPhotoBinding, EmptyViewModel>(), View.OnClickListener {


    private val mImagePaths by lazy {
        arrayListOf<LocalMedia?>()
    }

    override fun setContentView(): Int = R.layout.activity_photo

    override fun initData() {

    }

    override fun initListener() {
        mBinding.btnTakePhoto.setOnClickListener(this)
        mBinding.btnOpenGallery.setOnClickListener(this)
        mBinding.ivPic.setOnClickListener(this)
    }


    companion object {
        fun start(context: Context) {
            val intent = Intent(context, PhotoActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun onClick(v: View?) {
        when (v) {
            mBinding.btnTakePhoto -> { //拍照
                XXPermissions.with(this)
                    .permission(Permission.Group.STORAGE).request { allGranted, b ->
                        if (b) {
                            PictureSelector.create(this).openCamera(SelectMimeType.ofImage())
                                .forResult(object : OnResultCallbackListener<LocalMedia?> {

                                    override fun onCancel() {
                                        logcom("onCancel")
                                    }

                                    override fun onResult(result: ArrayList<LocalMedia?>?) {
                                        if (!result.isNullOrEmpty()) {
                                            mImagePaths.clear()
                                            mImagePaths.addAll(result)
                                            val filePath = result[0]?.availablePath
                                            val availablePath = result[0]!!.availablePath
                                            GlideUtil.load(mContext, filePath, mBinding.ivPic)
                                            logcom("onResult:$filePath")
                                            //绝对路径(当需要上传文件时用这里)
                                            val cameraFile = if (PictureMimeType.isContent(availablePath)) File(PictureFileUtils.getPath(mContext, Uri.parse(availablePath))) else File(availablePath)
                                            val absolutePath = cameraFile.absolutePath
                                        }
                                    }
                                })
                        }
                    }
            }
            mBinding.btnOpenGallery -> { //打开相册
                PictureSelector.create(this).openGallery(SelectMimeType.ofAll())
                    .setImageEngine(GlideEngine.createGlideEngine()).forResult(object :
                        OnResultCallbackListener<LocalMedia?> {

                        override fun onCancel() {
                            // 取消
                            logcom("onCancel")
                        }

                        override fun onResult(result: ArrayList<LocalMedia?>?) {
                            // 结果回调
                            if (!result.isNullOrEmpty()) {
                                mImagePaths.clear()
                                mImagePaths.addAll(result)
                                val filePath = result[0]?.availablePath
                                GlideUtil.load(mContext, filePath, mBinding.ivPic)
                                logcom("onResult:$filePath")
                            }
                        }
                    })
            }
            mBinding.ivPic -> { //预览图片
                PhotoHelper.previewPhoto(this, 0, mImagePaths)
            }
        }
    }
}